import { AudioClip, AudioSource, assert, warn, clamp01, assetManager, sys } from "cc";
export class AudioManager {

    private static _instance: AudioManager;
    private static _audioSource?: AudioSource;
    private static _cachedAudioClipMap: Record<string, AudioClip> = {};
    public static musicVolume: number = sys.localStorage.getItem('sound') ? Number(JSON.parse(sys.localStorage.getItem('sound')).musicVolume) : 1
    public static soundVolume: number = sys.localStorage.getItem('sound') ? Number(JSON.parse(sys.localStorage.getItem('sound')).soundVolume) : 1

    static get instance() {
        if (this._instance) {
            return this._instance;
        }

        this._instance = new AudioManager();
        return this._instance;
    }

    /**管理器初始化*/
    init(audioSource: AudioSource) {
        AudioManager._audioSource = audioSource;
    }

    /**
   * 播放音乐
   * @param {Boolean} loop 是否循环播放
   */
    public static playMusic() {
        const audioSource = AudioManager._audioSource!;
        assert(audioSource, 'AudioManager not inited!');

        // audioSource.loop = loop;
        if (!audioSource.playing) {
            audioSource.play();
        }
    }

    public static ptopMusic() {
        const audioSource = AudioManager._audioSource!;
        assert(audioSource, 'AudioManager not inited!');

        // audioSource.loop = loop;
        audioSource.stop();
    }


    /**
    * 播放音效
    * @param {String} name 音效名称
    * @param {Number} volumeScale 播放音量倍数
    */
    public static playSound(name: string) {
        const audioSource = AudioManager._audioSource!;
        assert(audioSource, 'AudioManager not inited!');

        const path = `audio/${name}`;
        let cachedAudioClip = AudioManager._cachedAudioClipMap[path];
        if (cachedAudioClip) {
            audioSource.playOneShot(cachedAudioClip, AudioManager.soundVolume);
        } else {
            assetManager.resources?.load(path, AudioClip, (err, clip) => {
                if (err) {
                    console.warn(err);
                    return;
                }

                AudioManager._cachedAudioClipMap[path] = clip;
                audioSource.playOneShot(clip, AudioManager.soundVolume);
            });
        }
    }
    // 设置音乐音量
    setMusicVolume(flag: number) {
        const audioSource = AudioManager._audioSource!;
        assert(audioSource, 'AudioManager not inited!');

        flag = clamp01(flag);
        audioSource.volume = flag;
    }
 
    // 音乐：静音 0 / 取消静音 1
    public static setMusicVolume(number): void {
        AudioManager.musicVolume = number;
        this.soundStatus()
        if (number === 1) {
            AudioManager.playMusic();
        } else {
            AudioManager.ptopMusic();
        }
        
    }
    // 音效：静音 0 / 取消静音 1
    public static setSoundVolume(number): void {
        AudioManager.soundVolume = number;
        this.soundStatus()
    }

    public static soundStatus () {
        const sound = {
            musicVolume: AudioManager.musicVolume,
            soundVolume: AudioManager.soundVolume
        }
        sys.localStorage.setItem('sound', JSON.stringify(sound))
    }

}
